package com.jblv.frame.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.Properties;

/**
 * @author happyonion.yangtao
 * @version 1.0
 * @title JblvFrameConfigUtils
 * @projectName jblv-frame
 * @packageName com.jblv.frame.common.utils
 * @description Jblv-Frame框架基础配置工具类
 * @date Create at 2020/5/22 15:35
 * @since jdk1.8
 */
public class JblvFrameConfigUtils {
    // 文件路径
    public static String FILE_NAME = "config/jblv-frame.properties";

    private static JblvFrameConfigUtils instance;

    private static final String TOKEN_EXPIRE_EXPIRE_TIME = "tokenExpireTime";

    // 配置文件的配置
    private Properties properties;

    /**
     * 私有构
     */
    private JblvFrameConfigUtils() {
        init();
    }

    /**
     * 初始化配置
     */
    /**
     * 初始化
     */
    private void init() {
        if (properties == null) {
            properties = new Properties();
        }
        String path = System.getProperty("user.dir") + File.separator + FILE_NAME;
        InputStream in = null;
        File file = new File(path);
        if (!file.exists()) {
//            path = JblvFrameConfigUtils.class.getClassLoader().getResource(FILE_NAME).getPath();
//            file = new File(path);
            in = JblvFrameConfigUtils.class.getClassLoader().getResourceAsStream(FILE_NAME);
        }
        try {
            //properties.load(new FileInputStream(file), "utf-8");
            properties.load(new InputStreamReader(in,"UTF-8"));
        } catch (IOException e) {
            System.out.println(FILE_NAME + " 文件不存在");
        }
    }

    /**
     * 创建单例
     *
     * @return
     */
    public static JblvFrameConfigUtils getInstance() {

        if (instance == null) {
            instance = new JblvFrameConfigUtils();
        }

        return instance;
    }

    public long getTokenExpireExpireTime(){
        String t = properties.getProperty(TOKEN_EXPIRE_EXPIRE_TIME);
        if(StringUtils.isEmpty(t)){
            return 0;
        }
        long tar = Long.parseLong(StringDealUtils.removeNonAscii(t));
        return tar;
    }

}
